package com.xr.miniprogram.controller.search;

import com.xr.common.core.controller.BaseController;
import com.xr.common.core.domain.AjaxResult;
import com.xr.miniprogram.domain.MiniProgramStandards;
import com.xr.miniprogram.domain.vo.MiniProgramSearchVO;
import com.xr.miniprogram.service.IMiniProgramStandardsService;
import io.swagger.annotations.*;
import org.apache.ibatis.annotations.Param;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * description:搜索控制
 *
 * @author: wls
 * @date: 2023/9/7 10:54
 * @version:1.0
 */
@Api("搜索控制")
@RestController
@RequestMapping("/miniprogram/search")
public class MiniProgramSearchController extends BaseController {

    @Resource
    RedisTemplate redisTemplate;

    @Resource
    IMiniProgramStandardsService standardsService;

    @ApiOperation("关键词检索规格信息列表")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "keyword", value="关键词", dataTypeClass = String.class),
        @ApiImplicitParam(name = "topic", value = "排序方式", dataTypeClass = String.class),
        @ApiImplicitParam(name = "userId", value = "用户id", dataTypeClass = String.class)
    })
    @RequestMapping("/get")
    public AjaxResult serviceList(@RequestBody Map<Object,String> params){
        String keyword = params.get("keyword");
        Long topic = Long.valueOf(params.get("topic"));
        String userId = params.get("userId");
        List<MiniProgramSearchVO> list = standardsService.searchAll(keyword,topic);
        HashMap<String, Object> stringObjectHashMap = new HashMap<>();
        stringObjectHashMap.put("keyword",keyword);
        stringObjectHashMap.put("list",list);
        if (userId==null){
            redisTemplate.opsForHash().put("keyword",keyword,list);
            return success(stringObjectHashMap);
        }else {
            HashSet<String> keywordsSet = (HashSet<String>) redisTemplate.opsForHash().get("keywords", userId);
            if (keywordsSet == null) {
                HashSet<String> strings = new HashSet<>();
                strings.add(keyword);
                redisTemplate.opsForHash().put("keywords", userId, strings);
            } else {
                keywordsSet.add(keyword);
                redisTemplate.opsForHash().put("keywords", userId, keywordsSet);
            }
            //设置历史查询过期时间
            redisTemplate.expire("keywords", 3, TimeUnit.DAYS);

            redisTemplate.opsForZSet().incrementScore("popularSearch", keyword, -1);
        }
        return success(stringObjectHashMap);

    }
    /**
     * 获取历史搜索
     * @return
     */
    @ApiOperation("获取历史搜索")
    @RequestMapping("/getSearchKey/{userId}")
    public AjaxResult keyList(@PathVariable(value = "userId",required = false)Long userId){
        String user = String.valueOf(userId);
        if (user==null){
            Set keyword = redisTemplate.opsForHash().keys("keyword");
            return success(keyword);
        }
        HashSet<String> keywordsSet = (HashSet<String>) redisTemplate.opsForHash().get("keywords", user);
        return success(keywordsSet);
    }

    /**
     * 获取热门搜索
     * @return
     */
    @ApiOperation("获取热门搜索")
    @RequestMapping("/getPopularSearch")
    public AjaxResult getPopularSearch(){
        Set popularSearch = redisTemplate.opsForZSet().range("popularSearch", 0, 5);
        return success(popularSearch);
    }
}
